<!-- eslint-disable vue/no-parsing-error -->
<!-- eslint-disable vue/valid-v-bind -->
<template>
  <div class="app-container">
    <div style="display: flex;flex-direction: row;justify-content: space-between; margin-bottom: .5rem;">
      <div v-if="leftButtonGroup" style="display: flex; flex-direction: row;justify-content: space-between;">
        <search-form
          v-if="searchForm.fields.length > 0"
          :search-form="searchForm"
          @searchFormEmit="searchFormEmit"
        />
        <operation-button :button-group="buttonGroup" />
      </div>
      <div v-if="rightButtonGroup" style="display: flex; flex-direction: row; ">
        <el-button-group>
          <el-button type="" icon="el-icon-refresh" :size="formSize" @click="getDataList" />
          <el-popover
            placement="bottom"
            trigger="click"
          >
            <el-button slot="reference" type="" icon="el-icon-menu" :size="formSize" />
            <div v-for="(item, index) in tableTitle" :key="index">
              <el-checkbox v-model="item.show" :label="item.label" size="large" />
            </div>
          </el-popover>
        </el-button-group>
      </div>
    </div>

    <div style="text-align: right">
      <pagination
        v-show="total > listQuery.pageSize"
        :total="total"
        :page.sync="listQuery.pageIndex"
        :limit.sync="listQuery.pageSize"
      />
    </div>

    <el-table
      v-if="showTable"
      ref="multipleTableRef"
      v-loading="loading"
      :data="tableData"
      :height="height"
      :header-cell-style="headerCellStyle"
      style="width: 100%"
      size="medium"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <!-- @sort-change="sortChange" -->
      <el-table-column v-if="multipleTable === true" type="selection" width="55" />
      <el-table-column
        v-for="(item, index) in filterTitles"
        :key="index"
        :prop="item.value"
        :label="item.label"
        :sortable="item.sortable === true"
        :fixed="item.fixed"
        :width="item.width"
        align="center"
      >
        <template v-slot="scope">
          <template v-if="item.type === 'slot'">
            <slot :name="item.slot" :row="scope.row" :column="item.prop" :index="scope.$index" />
          </template>
          <template v-else-if="item.type === 'router'">
            <router-link
              :to="{path: item.path, query: { ...item.query, ...getQuery(item.params, scope.row) }}"
              style="color: #409EFF"
            >
              <!-- Object.assign([], item.query, { [item.queryKey]: scope.row[item.queryValue] } ) -->
              <!-- onlyHasPhone: item.onlyHasPhone, groupQQ: scope.row.groupQq -->
              {{ scope.row[item.value] }}
            </router-link>
          </template>
          <template v-else-if="item.type === 'options'">
            <el-button
              v-for="(btn, btnKey) in item.options"
              :key="btnKey"
              :type="btn.type && btn.type !== '' ? btn.type: 'primary'"
              :icon="btn.icon ? btn.icon : ''"
              size="mini"
              style="margin: 2px; "
              @click="btn.callback(scope.row)"
            >
              {{ btn.text }}
            </el-button>
          </template>
          <!--   默认 text   -->
          <template v-else>
            {{ item.transfer ? item.transfer[scope.row[item.value]] : scope.row[item.value] }}
          </template>
        </template>
      </el-table-column>
    </el-table>

    <div style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.pageIndex"
        :limit.sync="listQuery.pageSize"
      />
    </div>
  </div>
</template>
<script>
import SearchForm from '@/components/SearchForm/index.vue'
import OperationButton from '@/components/OperationButton/index.vue'
import Pagination from '@/components/BasicTable/Pagination.vue'

import { jsonToParams } from '@/utils'
import { GetGroupMember } from '@/api/GroupMember'

export default {
  name: 'ImGroupMembers',
  components: { SearchForm, OperationButton, Pagination },
  props: {
    // 基本
    dataParams: {
      type: Object, default: () => {
      }
    },
    multipleTable: { type: Boolean, default: false },
    pagination: { type: Boolean, default: true },
    showTable: { type: Boolean, default: true },
    height: { type: Number, default: null },
    // eslint-disable-next-line vue/require-valid-default-prop
    headerCellStyle: {
      type: Object, default: function() {
        return { background: '#409EFF', color: '#fff' }
      }
    },
    // 特别操作
    isShowGroup: { type: Boolean, default: true },
    buttonGroup: { type: Object, default: null },
    leftButtonGroup: { type: Boolean, default: false },
    rightButtonGroup: { type: Boolean, default: true },
    reload: { type: Boolean, default: false }
  },
  data() {
    return {
      dataSource: () => GetGroupMember,
      loading: false,
      total: 0,
      temp: {
        orderBy: ''
      },
      tableData: null,
      listQuery: {
        pageIndex: 1,
        pageSize: 15
      },
      tableTitle: [
        {
          label: '用户名',
          value: 'UserName',
          show: true
        },
        {
          label: '昵称',
          value: 'NickName',
          show: true
        },
        {
          label: '状态',
          value: 'Status',
          show: true,
          transfer: [
            '禁用',
            '可用'
          ]
        },
        {
          label: '禁言时间',
          value: 'MuteTime',
          show: true,
          width: '200'
        },
        {
          label: '加入时间',
          value: 'JoinedON',
          show: true,
          width: '200'
        }
      ],
      searchForm: {
        expend: true,
        title: '表格筛选',
        fields: [
          {
            show: true,
            type: 'input',
            label: '用户名',
            labelShow: true,
            name: 'userName'
          },
          {
            show: true,
            type: 'input',
            label: '公司名称',
            labelShow: true,
            name: 'companyName'
          }
        ]
      }
    }
  },
  computed: {
    formSize() {
      return this.searchForm.size ? this.searchForm.size : 'medium'
    },
    filterTitles() {
      this.tableTitle.forEach((item, index) => {
        // eslint-disable-next-line no-constant-condition
        if (item.show === undefined) {
          this.tableTitle[index].show = true
        }
      })

      return this.tableTitle.filter(item => item.show !== false)
    }
  },
  watch: {
    listQuery: {
      handler(newVal, oldVal) {
        this.getDataList()
        this.$emit('closeReload', false)
      },
      deep: true
    },
    // 重载
    reload(newVal) {
      if (newVal) {
        this.getDataList()
      }
    },
    dataParams: {
      handler(newVal) {
        this.listQuery = { ...this.listQuery, ...this.dataParams }
      },
      deep: true
    }
  },
  created() {
    console.log('members', this.$route)
    if (this.$route.query !== undefined) {
      this.$route.meta.title = this.$route.query.groupName
      this.listQuery = { ...this.listQuery, ...this.$route.query }
    } else {
      this.getDataList()
    }
  },
  methods: {
    getQuery(params, row) {
      const p = {}
      for (const i in params) {
        p[i] = row[params[i]]
      }
      return p
    },
    rowClassName({ row, rowIndex }) {
      row.index = rowIndex
      row.xh = rowIndex + 1
    },
    searchFormEmit(v) {
      this.listQuery.pageIndex = 1
      this.listQuery = { ...this.listQuery, ...v }
    },
    handleSelectionChange(val) {
      this.$emit('batchDeleted', val)
    },
    // batchDeleted() {
    //   this.$emit('batchDeleted', this.selectDate)
    // },
    getDataList() {
      this.loading = true
      this.dataSource()(jsonToParams(this.listQuery)).then(res => {
        this.loading = false
        if (res.statusCode === 200) {
          if (res.data.DataList) {
            this.tableData = res.data.DataList
            this.total = res.data.RecordCount
          } else {
            this.tableData = res.data
            this.total = res.data.length
            console.log(this.tableData)
          }

          // reload
          this.$emit('closeReload', false)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
